function [data,objective]=ecogSpiderEcog2InputData(ecog)
% [data,objective]=ecogSpiderEcogData2SpiderData(ecog) Construct a spider data matrix from ecog data matrix 
%
% INPUT: 
% ecog:     Ecog data structure with fields
%           data: a channels by samples by trials (repetitions) matrix
%           selectedChannels: indices to the channels to include 
%                             (e.g. 2:64 would include channels 2 to 64)
%                             Default: all
%           selectedInterval: two element vector defining the time interval 
%                             to include (e.g. [0 200] would select the data
%                             in the inerval 0 and 200 ms as defined by 
%                             in ecog. timebase. Default: all
%           selectedTrials:   A vector with indices for the trials to use.
%                             Default: all
% OUTPUT:
% data:   A matrix of size length(selectedTrials) by length(selectedChannels)*length(samplesInSelectedInterval
%           Trial1:ch1(1),...,ch1(end),ch2(1),....ch2(end),...
%           Trial2:ch1(1),...,ch1(end),...
%           ....
% objective: A vector containing ones.  that can be class labes 

% 090501 JR wrote it



% input checks:
if ~isfield(ecog,'selectedChannels') || isempty(ecog.selectedChannels)
    ecog.selectedChannels=size(ecog.data,1);
end
if ~isfield(ecog,'selectedInterval') || isempty(ecog.selectedInterval)
    ecog.selectedInterval=[ecog.timebase(1) ecog.timebase(end)];
end
if ~isfield(ecog,'selectedTrials') || isempty(ecog.selectedTrials)
    ecog.selectedTrials=1:size(ecog.data,3);
end
%output size
idx=findNearest(ecog.selectedInterval,ecog.timebase);
data=zeros(length(ecog.selectedTrials),length(ecog.selectedChannels)*length(idx(1):idx(2))); 
objective=ones(1,length(ecog.selectedTrials));

%the first dataset
for k=1:length(ecog.selectedTrials)
    tmp=ecog.data(ecog.selectedChannels,idx(1):idx(2),ecog.selectedTrials(k))';
    data(k,:)=tmp(:);
end
% Here we have all trials as row vetors in a matrix
objective=ones(size(data,1),1);
